/*
 * $Id: Where.java 23 2002-09-29 13:59:30Z bpruessmann $
 *
 * Copyright (c) 2001 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.p4eclipse.internal.core.api;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import net.sourceforge.p4eclipse.core.api.ClientUser;
import net.sourceforge.p4eclipse.core.api.IPerforceCommand;
import net.sourceforge.p4eclipse.core.api.IWhereResult;
import net.sourceforge.p4eclipse.ui.PerforceUIPlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;

/**
  * ClientUser object for the 'p4 where' command.
  * 
  * @version 	$Revision: 23 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Where extends ClientUser implements IPerforceCommand
{
	private List result = new ArrayList();
	private IStatus status;
	private String clientRoot;
	
	/**
	  * Convenience method for creating instances of Where.
	  */
	public static final Where newInstance(String clientRoot)
	{
		MultiStatus status = new MultiStatus(PerforceUIPlugin.ID, IStatus.OK, "", null); //$NON-NLS-1$
		return new Where(status, clientRoot);
	}

	/**
	  * Creates a new instance.
	  */
	private Where(MultiStatus status, String clientRoot)
	{
		super(status);
		this.status = status;
		this.clientRoot = clientRoot;
	}	
	
	/**
	  * @see IClientUser#outputInfo(char, String)
	  */
	public void outputInfo(char level, String infoString)
	{
		if (level != '0')
			return;
			
		int firstStart = 0;
		int secondStart = infoString.indexOf("//", firstStart + 2); //$NON-NLS-1$
		int thirdStart = calculateThirdStart(infoString);		
			
		Result whereResult = new Result();
		whereResult.depotFilename = infoString.substring(firstStart, secondStart - 1);		
		whereResult.clientFilename = infoString.substring(secondStart, thirdStart - 1);
		whereResult.localFilename = infoString.substring(thirdStart);

		result.add(whereResult);
	}
	
	/** Calculates the position of the third (local) path. This is a little 
	  * tricky as we have to deal with "null" client roots.
	  */
	private int calculateThirdStart(String infoString)
	{		
		int secondStart = infoString.lastIndexOf("//");                         //$NON-NLS-1$
		
		int pos = infoString.lastIndexOf(File.separatorChar) + 1;
		String filename = "/" + infoString.substring(pos);
		
		pos = infoString.indexOf(filename, secondStart);
		
		return pos + filename.length() + 1;
	}

	/**
	  * Returns the name of the p4 command of this instance.
	  */
	public String getCommand()
	{
		return "where"; //$NON-NLS-1$
	}
	
	/**
	  * Returns the result of the operation.
	  */
	public IWhereResult[] getResult()
	{
		return (IWhereResult[])result.toArray(new IWhereResult[result.size()]);
	}

	/**
	  * Returns the IStatus object associated with the Fstat instance.
	  */
	public IStatus getStatus()
	{
		return status;
	}	
	
	/** @see IPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole()
	{
		return true;
	}	
	
	/**
	  * Implementation of the IWhereResult interface. Stores information
	  * provided by the 'p4 where' command. 
	  * 
	  * @see IWhereResult
	  */
	public class Result implements IWhereResult
	{
		private String depotFilename;
		private String clientFilename;
		private String localFilename;
	
		/** Creates a new Result object. */
		Result() { }
	
		/** @see IWhereResult#getDepotFilename() */
		public String getDepotFilename() { return depotFilename; }
	
		/** @see IWhereResult#getClientFilename() */
		public String getClientFilename() { return clientFilename; }
	
		/** @see IWhereResult#getLocalFilename() */
		public String getLocalFilename() { return localFilename; }
	}	
}
